home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / PowerPC / Dev / PPCRelease / Examples / R.Mainz / fastprim / embedelf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-28  |  1.5 KB  |  72 lines

  1.  
  2. /* ansi includes */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define ELFBYTES_PER_ROW (20)
  7.  
  8. int main( int ac, char **av )
  9. {
  10.     int   retval = EXIT_FAILURE;
  11.     FILE *fh;
  12.  
  13.     /* Input file name given ? */
  14.     if( ac == 2 )
  15.     {
  16.       /* Open ELF object file */
  17.       if( fh = fopen( av[ 1 ], "rb" ) )
  18.       {
  19.         char buffer[ ELFBYTES_PER_ROW ];
  20.         int  nbytes;
  21.  
  22.         /* Write intro */
  23.         printf( "\n"
  24.                 "/* ELF file %s embedded in C source */\n"
  25.                 "/* %s written by Roland Mainz, gisburn@w-specht.rhein-ruhr.de*/\n"
  26.                 "#include <stdlib.h>\n", av[ 1 ], av[ 0 ] );
  27.  
  28.         /* Write array introducer... */
  29.         printf( "char elfobject[] = \n{\n" );
  30.  
  31.         /* Process object... */
  32.         for( ; (nbytes = fread( buffer, 1, ELFBYTES_PER_ROW, fh )) > 0 ; )
  33.         {
  34.           int i;
  35.  
  36.           printf( "    \"" );
  37.  
  38.           for( i = 0 ; i < nbytes ; i++ )
  39.           {
  40.             printf( "\\x%02.2X", (int)buffer[ i ] );
  41.           }
  42.  
  43.           printf( "\"\n" );
  44.         }
  45.  
  46.  
  47.         if( !ferror( fh ) )
  48.         {
  49.           /* All done - write array terminator */
  50.           printf( "};\n\nsize_t elfobject_size = sizeof( elfobject );\n\n" );
  51.  
  52.           retval = EXIT_SUCCESS;
  53.         }
  54.         else
  55.         {
  56.           fprintf( stderr, "ERROR !!!\n" );
  57.         }
  58.  
  59.         (void)fclose( fh );
  60.       }
  61.     }
  62.     else
  63.     {
  64.       /* Usage... */
  65.       fprintf( stderr, "Usage: %s <elffile>\n", av[ 0 ] );
  66.     }
  67.  
  68.     return( retval );
  69. }
  70.  
  71.  
  72.